﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Catalog.Controllers
{
    public sealed class SupplierController : ExtController
    {
        #region GET/POST Actions

        [HttpGet]
        public ActionResult Index(char? azIdx, 
            int? page,
            string sortBy,
            bool? sortDesc)
        {
            sortBy = StringHelper.EnsureNotNull(sortBy).Trim()
                .ToLowerInvariant();
            page = page.HasValue ? page.Value : 0;
            dynamic m = new ExpandoObject();

            m.CountPerPage = Svc.Config(Par.In("WebPortal").Of(this).Group("Index").Name("Count"), 12);
            m.Index = Svc.ProcessOperation("SearchCatalogSuppliers", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                Term = new
                {
                    Expression = azIdx.HasValue && Char.IsLetterOrDigit(azIdx.Value) ? 
                        String.Format("{0}%", azIdx.Value) : 
                        String.Empty
                },
                Index = page * m.CountPerPage,
                Count = m.CountPerPage,
                FlagMask = "Active",
                SortBy = new [] 
                {
                    !String.IsNullOrEmpty(sortBy) && sortBy.Equals("rating")
                        ? "RatingAverage"
                        : "CreatedOn"
                },
                SortDir = !sortDesc.HasValue || sortDesc.Value
                    ? "Descending"
                    : "Ascending"
            }, new
            {
                DisclosureLevel = "Extended",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return View(m);
        }

        [HttpGet, RestoreState]
        public ActionResult Show(int id)
        {
            dynamic m = Svc.ProcessOperation("GetCatalogSupplier", new
            {
                EntityID = id
            }, new
            {
                DisclosureLevel = "Entire",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            if(m == null)
                return HttpNotFound();
            return View(m);
        }

        #endregion

        #region Childs Actions

        [ChildActionOnly]
        public ActionResult _Overview(int id)
        {
            dynamic m = Svc.ProcessOperation("GetCatalogSupplier", new
            {
                EntityID = id
            }, new
            {
                DisclosureLevel = "Extended",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return PartialView(m);
        }

        [ChildActionOnly]
        public ActionResult _Promoted()
        {
            dynamic m = Svc.ProcessOperation("SearchCatalogSuppliers", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                Count = Svc.Config(Par.In("WebPortal").Of(this).Group("_Latest").Name("Count"), 12),
                FlagMask = "Active,Promoted",
                SortBy = new[] 
                {
                    "CreatedOn"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Extended",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            return PartialView(m);
        }

        [ChildActionOnly]
        public ActionResult _Comments(int id)
        {
            dynamic m = new ExpandoObject();

            m.EntityID = id;
            m.Comments = Svc.ProcessOperation("SearchCatalogSupplierComments", new
            {
                HolderID = id
            }, new
            {
                DisclosureLevel = "Extended"
            }).Items;
            return PartialView(m);
        }

        #endregion
    }
}
